# Initialize ----
source('src/initialize.R')

# 3 Preregistration Deviation ----
## 3.2 Preregistered Analyses ----
### 3.2.1 ANOVA with Tukey HSD comparisons ----
#### Table 1 (DV: Thermometer) ----
##### Full Sample ----
anova211 = aov(therm ~ treatment, ds)
anova211.1 = glht(anova211, linfct = mcp(treatment = "Tukey"))
anova211.2 = summary(anova211.1)$test
anova211.2lwr = format(
  round(as.data.frame(confint(anova211.1)$confint)$lwr, 2),
  nsmall = 2
)
anova211.2upr = format(
  round(as.data.frame(confint(anova211.1)$confint)$upr, 2),
  nsmall = 2
)
anova211.2ci = paste0("[", anova211.2lwr, ", ", anova211.2upr, "]")
anova211.3.full <- cbind(
  round(anova211.2$coefficients, 2),
  round(anova211.2$sigma, 2),
  anova211.2ci,
  format(round(anova211.2$pvalues, 2), nsmall = 2)
)
colnames(anova211.3.full) <- c(
  "Estimate",
  "Std. Error",
  "95% Confidence Interval",
  "p-value"
)
anova211.3.full

##### Republican Sample ----
anova211 = aov(therm ~ treatment, ds %>% filter(pid == 'rep'))
anova211.1 = glht(anova211, linfct = mcp(treatment = "Tukey"))
anova211.2 = summary(anova211.1)$test
anova211.2lwr = format(
  round(as.data.frame(confint(anova211.1)$confint)$lwr, 2),
  nsmall = 2
)
anova211.2upr = format(
  round(as.data.frame(confint(anova211.1)$confint)$upr, 2),
  nsmall = 2
)
anova211.2ci = paste0("[", anova211.2lwr, ", ", anova211.2upr, "]")
anova211.3.rep <- cbind(
  round(anova211.2$coefficients, 2),
  round(anova211.2$sigma, 2),
  anova211.2ci,
  format(round(anova211.2$pvalues, 2), nsmall = 2)
)
colnames(anova211.3.rep) <- c(
  "Estimate",
  "Std. Error",
  "95% Confidence Interval",
  "p-value"
)
anova211.3.rep

##### Independent Sample ----
anova211 = aov(therm ~ treatment, ds %>% filter(pid == 'ind'))
anova211.1 = glht(anova211, linfct = mcp(treatment = "Tukey"))
anova211.2 = summary(anova211.1)$test
anova211.2lwr = format(
  round(as.data.frame(confint(anova211.1)$confint)$lwr, 2),
  nsmall = 2
)
anova211.2upr = format(
  round(as.data.frame(confint(anova211.1)$confint)$upr, 2),
  nsmall = 2
)
anova211.2ci = paste0("[", anova211.2lwr, ", ", anova211.2upr, "]")
anova211.3.ind <- cbind(
  round(anova211.2$coefficients, 2),
  round(anova211.2$sigma, 2),
  anova211.2ci,
  format(round(anova211.2$pvalues, 2), nsmall = 2)
)
colnames(anova211.3.ind) <- c(
  "Estimate",
  "Std. Error",
  "95% Confidence Interval",
  "p-value"
)
anova211.3.ind

##### Democratic Sample ----
anova211 = aov(therm ~ treatment, ds %>% filter(pid == 'dem'))
anova211.1 = glht(anova211, linfct = mcp(treatment = "Tukey"))
anova211.2 = summary(anova211.1)$test
anova211.2lwr = format(
  round(as.data.frame(confint(anova211.1)$confint)$lwr, 2),
  nsmall = 2
)
anova211.2upr = format(
  round(as.data.frame(confint(anova211.1)$confint)$upr, 2),
  nsmall = 2
)
anova211.2ci = paste0("[", anova211.2lwr, ", ", anova211.2upr, "]")
anova211.3.dem <- cbind(
  round(anova211.2$coefficients, 2),
  round(anova211.2$sigma, 2),
  anova211.2ci,
  format(round(anova211.2$pvalues, 2), nsmall = 2)
)
colnames(anova211.3.dem) <- c(
  "Estimate",
  "Std. Error",
  "95% Confidence Interval",
  "p-value"
)
anova211.3.dem

#### Table 2 (DV: Perceived Ideology) ----
##### Full Sample ----
anova212 = aov(cand_ideo ~ treatment, ds)
anova212.1 = glht(anova212, linfct = mcp(treatment = "Tukey"))
anova212.2 = summary(anova212.1)$test
anova212.2lwr = format(
  round(as.data.frame(confint(anova212.1)$confint)$lwr, 2),
  nsmall = 2
)
anova212.2upr = format(
  round(as.data.frame(confint(anova212.1)$confint)$upr, 2),
  nsmall = 2
)
anova212.2ci = paste0("[", anova212.2lwr, ", ", anova212.2upr, "]")
anova212.3.full <- cbind(
  round(anova212.2$coefficients, 2),
  round(anova212.2$sigma, 2),
  anova212.2ci,
  format(round(anova212.2$pvalues, 2), nsmall = 2)
)
colnames(anova212.3.full) <- c(
  "Estimate",
  "Std. Error",
  "95% Confidence Interval",
  "p-value"
)
anova212.3.full

##### Republican Sample ----
anova212 = aov(cand_ideo ~ treatment, ds %>% filter(pid == 'rep'))
anova212.1 = glht(anova212, linfct = mcp(treatment = "Tukey"))
anova212.2 = summary(anova212.1)$test
anova212.2lwr = format(
  round(as.data.frame(confint(anova212.1)$confint)$lwr, 2),
  nsmall = 2
)
anova212.2upr = format(
  round(as.data.frame(confint(anova212.1)$confint)$upr, 2),
  nsmall = 2
)
anova212.2ci = paste0("[", anova212.2lwr, ", ", anova212.2upr, "]")
anova212.3.rep <- cbind(
  round(anova212.2$coefficients, 2),
  round(anova212.2$sigma, 2),
  anova212.2ci,
  format(round(anova212.2$pvalues, 2), nsmall = 2)
)
colnames(anova212.3.rep) <- c(
  "Estimate",
  "Std. Error",
  "95% Confidence Interval",
  "p-value"
)
anova212.3.rep

##### Independent Sample ----
anova212 = aov(cand_ideo ~ treatment, ds %>% filter(pid == 'ind'))
anova212.1 = glht(anova212, linfct = mcp(treatment = "Tukey"))
anova212.2 = summary(anova212.1)$test
anova212.2lwr = format(
  round(as.data.frame(confint(anova212.1)$confint)$lwr, 2),
  nsmall = 2
)
anova212.2upr = format(
  round(as.data.frame(confint(anova212.1)$confint)$upr, 2),
  nsmall = 2
)
anova212.2ci = paste0("[", anova212.2lwr, ", ", anova212.2upr, "]")
anova212.3.ind <- cbind(
  round(anova212.2$coefficients, 2),
  round(anova212.2$sigma, 2),
  anova212.2ci,
  format(round(anova212.2$pvalues, 2), nsmall = 2)
)
colnames(anova212.3.ind) <- c(
  "Estimate",
  "Std. Error",
  "95% Confidence Interval",
  "p-value"
)
anova212.3.ind

##### Democratic Sample ----
anova212 = aov(cand_ideo ~ treatment, ds %>% filter(pid == 'dem'))
anova212.1 = glht(anova212, linfct = mcp(treatment = "Tukey"))
anova212.2 = summary(anova212.1)$test
anova212.2lwr = format(
  round(as.data.frame(confint(anova212.1)$confint)$lwr, 2),
  nsmall = 2
)
anova212.2upr = format(
  round(as.data.frame(confint(anova212.1)$confint)$upr, 2),
  nsmall = 2
)
anova212.2ci = paste0("[", anova212.2lwr, ", ", anova212.2upr, "]")
anova212.3.dem <- cbind(
  round(anova212.2$coefficients, 2),
  round(anova212.2$sigma, 2),
  anova212.2ci,
  format(round(anova212.2$pvalues, 2), nsmall = 2)
)
colnames(anova212.3.dem) <- c(
  "Estimate",
  "Std. Error",
  "95% Confidence Interval",
  "p-value"
)
anova212.3.dem

### 3.2.2 OLS Regression Models ----
#### Table A3 (DV: Thermometer) ----
##### Full Sample ----
summary(
  lm(
    therm ~
      t_te *
        t_si +
        pid +
        ideo +
        trump16 +
        trump20 +
        male +
        educ +
        age +
        white +
        hispanic,
    ds
  )
)

##### Republican Sample ----
summary(
  lm(
    therm ~
      t_te *
        t_si +
        ideo +
        trump16 +
        trump20 +
        male +
        educ +
        age +
        white +
        hispanic,
    ds %>% filter(pid == 'rep')
  )
)

##### Independent Sample ----
summary(
  lm(
    therm ~
      t_te *
        t_si +
        ideo +
        trump16 +
        trump20 +
        male +
        educ +
        age +
        white +
        hispanic,
    ds %>% filter(pid == 'ind')
  )
)

##### Democratic Sample ----
summary(
  lm(
    therm ~
      t_te *
        t_si +
        ideo +
        trump16 +
        trump20 +
        male +
        educ +
        age +
        white +
        hispanic,
    ds %>% filter(pid == 'dem')
  )
)

##### Conservative Sample ----
summary(
  lm(
    therm ~
      t_te *
        t_si +
        pid +
        trump16 +
        trump20 +
        male +
        educ +
        age +
        white +
        hispanic,
    ds %>% filter(ideo < 3)
  )
)

##### Moderate Sample ----
summary(
  lm(
    therm ~
      t_te *
        t_si +
        pid +
        trump16 +
        trump20 +
        male +
        educ +
        age +
        white +
        hispanic,
    ds %>% filter(ideo == 3)
  )
)

##### Liberal Sample ---
summary(
  lm(
    therm ~
      t_te *
        t_si +
        pid +
        trump16 +
        trump20 +
        male +
        educ +
        age +
        white +
        hispanic,
    ds %>% filter(ideo > 3)
  )
)

##### Male sample ----
summary(
  lm(
    therm ~
      t_te *
        t_si +
        ideo +
        pid +
        trump16 +
        trump20 +
        educ +
        age +
        white +
        hispanic,
    ds %>% filter(male == 1)
  )
)

##### Not male sample ----
summary(
  lm(
    therm ~
      t_te *
        t_si +
        ideo +
        pid +
        trump16 +
        trump20 +
        educ +
        age +
        white +
        hispanic,
    ds %>% filter(male == 0)
  )
)

##### Bachelors or higher sample ----
summary(
  lm(
    therm ~
      t_te *
        t_si +
        ideo +
        pid +
        trump16 +
        trump20 +
        male +
        age +
        white +
        hispanic,
    ds %>% filter(educ >= 5)
  )
)

##### Less than Bachelors sample ----
summary(
  lm(
    therm ~
      t_te *
        t_si +
        ideo +
        pid +
        trump16 +
        trump20 +
        male +
        age +
        white +
        hispanic,
    ds %>% filter(educ >= 5)
  )
)


#### Table A4 (DV: Perceived Ideology) ----
##### Full Sample ----
summary(
  lm(
    cand_ideo ~
      t_te *
        t_si +
        pid +
        ideo +
        trump16 +
        trump20 +
        male +
        educ +
        age +
        white +
        hispanic,
    ds
  )
)

##### Republican Sample ----
summary(
  lm(
    cand_ideo ~
      t_te *
        t_si +
        ideo +
        trump16 +
        trump20 +
        male +
        educ +
        age +
        white +
        hispanic,
    ds %>% filter(pid == 'rep')
  )
)

##### Independent Sample ----
summary(
  lm(
    cand_ideo ~
      t_te *
        t_si +
        ideo +
        trump16 +
        trump20 +
        male +
        educ +
        age +
        white +
        hispanic,
    ds %>% filter(pid == 'ind')
  )
)

##### Democratic Sample ----
summary(
  lm(
    cand_ideo ~
      t_te *
        t_si +
        ideo +
        trump16 +
        trump20 +
        male +
        educ +
        age +
        white +
        hispanic,
    ds %>% filter(pid == 'dem')
  )
)

##### Conservative Sample ----
summary(
  lm(
    cand_ideo ~
      t_te *
        t_si +
        pid +
        trump16 +
        trump20 +
        male +
        educ +
        age +
        white +
        hispanic,
    ds %>% filter(ideo < 3)
  )
)

##### Moderate Sample ----
summary(
  lm(
    cand_ideo ~
      t_te *
        t_si +
        pid +
        trump16 +
        trump20 +
        male +
        educ +
        age +
        white +
        hispanic,
    ds %>% filter(ideo == 3)
  )
)

##### Liberal Sample ---
summary(
  lm(
    cand_ideo ~
      t_te *
        t_si +
        pid +
        trump16 +
        trump20 +
        male +
        educ +
        age +
        white +
        hispanic,
    ds %>% filter(ideo > 3)
  )
)

##### Male sample ----
summary(
  lm(
    cand_ideo ~
      t_te *
        t_si +
        ideo +
        pid +
        trump16 +
        trump20 +
        educ +
        age +
        white +
        hispanic,
    ds %>% filter(male == 1)
  )
)

##### Not male sample ----
summary(
  lm(
    cand_ideo ~
      t_te *
        t_si +
        ideo +
        pid +
        trump16 +
        trump20 +
        educ +
        age +
        white +
        hispanic,
    ds %>% filter(male == 0)
  )
)

##### Bachelors or higher sample ----
summary(
  lm(
    cand_ideo ~
      t_te *
        t_si +
        ideo +
        pid +
        trump16 +
        trump20 +
        male +
        age +
        white +
        hispanic,
    ds %>% filter(educ >= 5)
  )
)

##### Less than Bachelors sample ----
summary(
  lm(
    cand_ideo ~
      t_te *
        t_si +
        ideo +
        pid +
        trump16 +
        trump20 +
        male +
        age +
        white +
        hispanic,
    ds %>% filter(educ >= 5)
  )
)

#### Table A5 (DV: Thermometer, Ind Leaners) ----
##### Full sample ----
summary(
  lm(
    therm ~
      t_te *
        t_si +
        pid_alt +
        ideo +
        trump16 +
        trump20 +
        male +
        educ +
        age +
        white +
        hispanic,
    ds
  )
)

##### Republican sample ----
summary(
  lm(
    therm ~
      t_te *
        t_si +
        ideo +
        trump16 +
        trump20 +
        male +
        educ +
        age +
        white +
        hispanic,
    ds %>% filter(pid_alt == 'rep')
  )
)

##### Independent sample ----
summary(
  lm(
    therm ~
      t_te *
        t_si +
        ideo +
        trump16 +
        trump20 +
        male +
        educ +
        age +
        white +
        hispanic,
    ds %>% filter(pid_alt == 'ind')
  )
)

##### Democratic sample ----
summary(
  lm(
    therm ~
      t_te *
        t_si +
        ideo +
        trump16 +
        trump20 +
        male +
        educ +
        age +
        white +
        hispanic,
    ds %>% filter(pid_alt == 'dem')
  )
)

##### Conservative sample ----
summary(
  lm(
    therm ~
      t_te *
        t_si +
        pid_alt +
        trump16 +
        trump20 +
        male +
        educ +
        age +
        white +
        hispanic,
    ds %>% filter(ideo < 3)
  )
)

##### Moderate sample ----
summary(
  lm(
    therm ~
      t_te *
        t_si +
        pid_alt +
        trump16 +
        trump20 +
        male +
        educ +
        age +
        white +
        hispanic,
    ds %>% filter(ideo == 3)
  )
)

##### Liberal sample ----
summary(
  lm(
    therm ~
      t_te *
        t_si +
        pid_alt +
        trump16 +
        trump20 +
        male +
        educ +
        age +
        white +
        hispanic,
    ds %>% filter(ideo > 3)
  )
)

##### Male sample ----
summary(
  lm(
    therm ~
      t_te *
        t_si +
        ideo +
        pid_alt +
        trump16 +
        trump20 +
        educ +
        age +
        white +
        hispanic,
    ds %>% filter(male == 1)
  )
)

##### Not male sample ----
summary(
  lm(
    therm ~
      t_te *
        t_si +
        ideo +
        pid_alt +
        trump16 +
        trump20 +
        educ +
        age +
        white +
        hispanic,
    ds %>% filter(male == 0)
  )
)

##### Bachelors or higher sample ----
summary(
  lm(
    therm ~
      t_te *
        t_si +
        ideo +
        pid_alt +
        trump16 +
        trump20 +
        male +
        age +
        white +
        hispanic,
    ds %>% filter(educ >= 5)
  )
)

##### Less than Bachelors sample ----
summary(
  lm(
    therm ~
      t_te *
        t_si +
        ideo +
        pid_alt +
        trump16 +
        trump20 +
        male +
        age +
        white +
        hispanic,
    ds %>% filter(educ < 5)
  )
)

#### Table A6 (DV: Perceived Ideology, Ind Leaners) ----
##### Full sample ----
summary(
  lm(
    cand_ideo ~
      t_te *
        t_si +
        pid_alt +
        ideo +
        trump16 +
        trump20 +
        male +
        educ +
        age +
        white +
        hispanic,
    ds
  )
)

##### Republican sample ----
summary(
  lm(
    cand_ideo ~
      t_te *
        t_si +
        ideo +
        trump16 +
        trump20 +
        male +
        educ +
        age +
        white +
        hispanic,
    ds %>% filter(pid_alt == 'rep')
  )
)

##### Independent sample ----
summary(
  lm(
    cand_ideo ~
      t_te *
        t_si +
        ideo +
        trump16 +
        trump20 +
        male +
        educ +
        age +
        white +
        hispanic,
    ds %>% filter(pid_alt == 'ind')
  )
)

##### Democratic sample ----
summary(
  lm(
    cand_ideo ~
      t_te *
        t_si +
        ideo +
        trump16 +
        trump20 +
        male +
        educ +
        age +
        white +
        hispanic,
    ds %>% filter(pid_alt == 'dem')
  )
)

##### Conservative sample ----
summary(
  lm(
    cand_ideo ~
      t_te *
        t_si +
        pid_alt +
        trump16 +
        trump20 +
        male +
        educ +
        age +
        white +
        hispanic,
    ds %>% filter(ideo < 3)
  )
)

##### Moderate sample ----
summary(
  lm(
    cand_ideo ~
      t_te *
        t_si +
        pid_alt +
        trump16 +
        trump20 +
        male +
        educ +
        age +
        white +
        hispanic,
    ds %>% filter(ideo == 3)
  )
)

##### Liberal sample ----
summary(
  lm(
    cand_ideo ~
      t_te *
        t_si +
        pid_alt +
        trump16 +
        trump20 +
        male +
        educ +
        age +
        white +
        hispanic,
    ds %>% filter(ideo > 3)
  )
)

##### Male sample ----
summary(
  lm(
    cand_ideo ~
      t_te *
        t_si +
        ideo +
        pid_alt +
        trump16 +
        trump20 +
        educ +
        age +
        white +
        hispanic,
    ds %>% filter(male == 1)
  )
)

##### Not male sample ----
summary(
  lm(
    cand_ideo ~
      t_te *
        t_si +
        ideo +
        pid_alt +
        trump16 +
        trump20 +
        educ +
        age +
        white +
        hispanic,
    ds %>% filter(male == 0)
  )
)

##### Bachelors or higher sample ----
summary(
  lm(
    cand_ideo ~
      t_te *
        t_si +
        ideo +
        pid_alt +
        trump16 +
        trump20 +
        male +
        age +
        white +
        hispanic,
    ds %>% filter(educ >= 5)
  )
)

##### Less than Bachelors sample ----
summary(
  lm(
    cand_ideo ~
      t_te *
        t_si +
        ideo +
        pid_alt +
        trump16 +
        trump20 +
        male +
        age +
        white +
        hispanic,
    ds %>% filter(educ < 5)
  )
)

## 3.3 Exploratory Analyses ----
### 3.3.1 Alternative Outcome: Vote Intention (Candidate) ----
#### Figure A1 ----
ds %>%
  mutate(
    t_te = factor(t_te),
    t_si = factor(
      t_si,
      levels = 0:1,
      labels = c('Moderately\nConservative', 'Extremely\nConservative')
    )
  ) %>%
  group_by(t_te, t_si) %>%
  summarise(
    mean = mean(vote_cand, na.rm = TRUE),
    se = sd(vote_cand, na.rm = TRUE) / sqrt(length(vote_cand)),
    lower = mean - 1.96 * se,
    upper = mean + 1.96 * se
  ) %>%
  ggplot(
    aes(
      x = t_te,
      y = mean,
      ymin = lower,
      ymax = upper
    )
  ) +
  geom_errorbar(width = 0) +
  geom_point() +
  geom_text(
    aes(y = lower, label = format(round(mean, 2), nsmall = 2)),
    vjust = 2
  ) +
  facet_wrap(~t_si) +
  scale_y_continuous(limits = c(1, 5)) +
  labs(
    x = "Trump Endorsement",
    y = "Intention to Vote for Candidate\n(1 = Very unlikely, 5 = Very likely)"
  ) +
  theme_bw()

#### Table A7 ----
t.test(ds$vote_cand[ds$t_te == 1], ds$vote_cand[ds$t_te == 0])
t.test(
  ds$vote_cand[ds$t_te == 1 & ds$t_si == 0],
  ds$vote_cand[ds$t_te == 0 & ds$t_si == 0]
)
t.test(
  ds$vote_cand[ds$t_te == 1 & ds$t_si == 1],
  ds$vote_cand[ds$t_te == 0 & ds$t_si == 1]
)

t.test(
  ds$vote_cand[ds$t_te == 1 & ds$pid == "rep"],
  ds$vote_cand[ds$t_te == 0 & ds$pid == "rep"]
)
t.test(
  ds$vote_cand[ds$t_te == 1 & ds$pid == "ind"],
  ds$vote_cand[ds$t_te == 0 & ds$pid == "ind"]
)
t.test(
  ds$vote_cand[ds$t_te == 1 & ds$pid == "dem"],
  ds$vote_cand[ds$t_te == 0 & ds$pid == "dem"]
)

#### Table A8 ----
mod.votecand <- lm(vote_cand ~ t_te * t_si * pid * ideo, ds)
summary(margins(mod.votecand, variables = "t_te"))
summary(margins(mod.votecand, variables = "t_te", at = list(t_si = 0:1)))
summary(margins(
  mod.votecand,
  variables = "t_te",
  at = list(pid = c("rep", "ind", "dem"))
))

### 3.3.2 Alternative Outcome: Vote Enthusiasm ----
#### Figure A2 ----
ds %>%
  mutate(
    t_te = factor(t_te),
    t_si = factor(
      t_si,
      levels = 0:1,
      labels = c('Moderately\nConservative', 'Extremely\nConservative')
    )
  ) %>%
  group_by(t_te, t_si) %>%
  summarise(
    mean = mean(vote_enth, na.rm = TRUE),
    se = sd(vote_enth, na.rm = TRUE) / sqrt(length(vote_enth)),
    lower = mean - 1.96 * se,
    upper = mean + 1.96 * se
  ) %>%
  ggplot(
    aes(
      x = t_te,
      y = mean,
      ymin = lower,
      ymax = upper
    )
  ) +
  geom_errorbar(width = 0) +
  geom_point() +
  geom_text(
    aes(y = lower, label = format(round(mean, 2), nsmall = 2)),
    vjust = 2
  ) +
  facet_wrap(~t_si) +
  scale_y_continuous(limits = c(1, 3)) +
  labs(
    x = "Trump Endorsement",
    y = "Enthusiasm to Vote\n(1 = Less Enthusiastic, 3 = More Enthusiastic)"
  ) +
  theme_bw()

#### Table A9 ----
t.test(ds$vote_enth[ds$t_te == 1], ds$vote_enth[ds$t_te == 0])
t.test(
  ds$vote_enth[ds$t_te == 1 & ds$t_si == 0],
  ds$vote_enth[ds$t_te == 0 & ds$t_si == 0]
)
t.test(
  ds$vote_enth[ds$t_te == 1 & ds$t_si == 1],
  ds$vote_enth[ds$t_te == 0 & ds$t_si == 1]
)

t.test(
  ds$vote_enth[ds$t_te == 1 & ds$pid == "rep"],
  ds$vote_enth[ds$t_te == 0 & ds$pid == "rep"]
)
t.test(
  ds$vote_enth[ds$t_te == 1 & ds$pid == "ind"],
  ds$vote_enth[ds$t_te == 0 & ds$pid == "ind"]
)
t.test(
  ds$vote_enth[ds$t_te == 1 & ds$pid == "dem"],
  ds$vote_enth[ds$t_te == 0 & ds$pid == "dem"]
)

#### Table A10 ----
mod.voteenth <- lm(vote_enth ~ t_te * t_si * pid * ideo, ds)
summary(margins(mod.voteenth, variables = "t_te"))
summary(margins(mod.voteenth, variables = "t_te", at = list(t_si = 0:1)))
summary(margins(
  mod.voteenth,
  variables = "t_te",
  at = list(pid = c("rep", "ind", "dem"))
))

### 3.3.3 Alternative Outcome: Vote Intention (General) ----
#### Figure A3 ----
ds %>%
  group_by(t_te, t_si) %>%
  mutate(
    t_te = factor(t_te),
    t_si = factor(
      t_si,
      levels = 0:1,
      labels = c('Moderately\nConservative', 'Extremely\nConservative')
    )
  ) %>%
  summarise(
    mean = mean(vote_gen, na.rm = TRUE),
    se = sd(vote_gen, na.rm = TRUE) / sqrt(length(vote_gen)),
    lower = mean - 1.96 * se,
    upper = mean + 1.96 * se
  ) %>%
  ggplot(
    aes(
      x = t_te,
      y = mean,
      ymin = lower,
      ymax = upper
    )
  ) +
  geom_errorbar(width = 0) +
  geom_point() +
  geom_text(
    aes(y = lower, label = format(round(mean, 2), nsmall = 2)),
    vjust = 2
  ) +
  facet_wrap(~t_si) +
  scale_y_continuous(limits = c(0, 1)) +
  labs(
    x = "Trump Endorsement",
    y = "Intend to Vote in General\n(1 = Yes, 0 = No/Unsure)"
  ) +
  theme_bw()

#### Table A11 ----
t.test(ds$vote_gen[ds$t_te == 1], ds$vote_gen[ds$t_te == 0])
t.test(
  ds$vote_gen[ds$t_te == 1 & ds$t_si == 0],
  ds$vote_gen[ds$t_te == 0 & ds$t_si == 0]
)
t.test(
  ds$vote_gen[ds$t_te == 1 & ds$t_si == 1],
  ds$vote_gen[ds$t_te == 0 & ds$t_si == 1]
)

t.test(
  ds$vote_gen[ds$t_te == 1 & ds$pid == "rep"],
  ds$vote_gen[ds$t_te == 0 & ds$pid == "rep"]
)
t.test(
  ds$vote_gen[ds$t_te == 1 & ds$pid == "ind"],
  ds$vote_gen[ds$t_te == 0 & ds$pid == "ind"]
)
t.test(
  ds$vote_gen[ds$t_te == 1 & ds$pid == "dem"],
  ds$vote_gen[ds$t_te == 0 & ds$pid == "dem"]
)

#### Table A12 ----
mod.votegen <- lm(vote_gen ~ t_te * t_si * pid * ideo, ds)
summary(margins(mod.votegen, variables = "t_te"))
summary(margins(mod.votegen, variables = "t_te", at = list(t_si = 0:1)))
summary(margins(
  mod.votegen,
  variables = "t_te",
  at = list(pid = c("rep", "ind", "dem"))
))

# 4 Sample Descriptions ----
## 4.1 Table A13 Descriptive Statistics ----
datasummary(
  Heading("Male") *
    male +
    Heading("Education Level") * educ +
    Heading("Age") * age +
    Heading("Race: White") * white +
    Heading("Ethnicity: Hispanic") * hispanic +
    Heading("Ideology (7 = Conservative)") * ideo +
    Heading("Voted for Trump in 2016") * trump16 +
    Heading("Voted for Trump in 2020") * trump20 ~
    pid * (Mean + SD + Min + Median + Max),
  ds %>%
    mutate(
      pid = case_when(
        pid == 'rep' ~ 'Republicans',
        pid == 'ind' ~ 'Independents',
        pid == 'dem' ~ 'Democrats'
      )
    ),
  fmt = 2,
  #notes = c('A note at the bottom of the table.'),
  add_rows = data.frame(
    'Party Identification (Count)',
    '375',
    '',
    '',
    '',
    '',
    '269',
    '',
    '',
    '',
    '',
    '248',
    '',
    '',
    '',
    ''
  )
)

## 4.2 Balance tests ----
#### Table A14 (DV = Thermometer, Demographic) ----
ds %>%
  dplyr::select(
    t_te,
    male,
    educ,
    age,
    white,
    hispanic
  ) %>%
  tbl_summary(
    by = t_te,
    missing = 'no',
    label = c(
      male ~ 'Sex: Male',
      educ ~ 'Education Attainment',
      age ~ 'Age',
      white ~ 'Race: White',
      hispanic ~ 'Ethnicity: Hispanic'
    )
  ) %>%
  add_p() %>%
  italicize_levels() %>%
  modify_header(label ~ "**Variable**") %>%
  as_gt()

#### Table A15 (DV = Thermometer, Political) ----
ds %>%
  dplyr::select(
    t_te,
    ideo,
    pid,
    trump16,
    trump20
  ) %>%
  tbl_summary(
    by = t_te,
    missing = 'no',
    label = c(
      ideo ~ 'Ideology',
      pid ~ 'Party Affiliation',
      trump16 ~ 'Voted for Trump in 2016',
      trump20 ~ 'Voted for Trump in 2020'
    )
  ) %>%
  add_p() %>%
  italicize_levels() %>%
  modify_header(label ~ "**Variable**") %>%
  as_gt()

#### Table A16 (DV = Perceived Ideology, Demographic) ----
ds %>%
  dplyr::select(
    t_si,
    male,
    educ,
    age,
    white,
    hispanic
  ) %>%
  tbl_summary(
    by = t_si,
    missing = 'no',
    label = c(
      male ~ 'Sex: Male',
      educ ~ 'Education Attainment',
      age ~ 'Age',
      white ~ 'Race: White',
      hispanic ~ 'Ethnicity: Hispanic'
    )
  ) %>%
  add_p() %>%
  italicize_levels() %>%
  modify_header(label ~ "**Variable**") %>%
  as_gt()

#### Table A17 (DV = Perceived Ideology, Political) ----
ds %>%
  dplyr::select(
    t_si,
    ideo,
    pid,
    trump16,
    trump20
  ) %>%
  tbl_summary(
    by = t_si,
    missing = 'no',
    label = c(
      ideo ~ 'Ideology',
      pid ~ 'Party Affiliation',
      trump16 ~ 'Voted for Trump in 2016',
      trump20 ~ 'Voted for Trump in 2020'
    )
  ) %>%
  add_p() %>%
  italicize_levels() %>%
  modify_header(label ~ "**Variable**") %>%
  as_gt()

# 5 Experimental Analysis ----
## 5.1 Visual mean comparisons ----
#### Figure A4 (DV = Thermometer, by = ideo) ----
anno.df3 <-
  data.frame(
    x1 = rep(1, 6),
    x2 = rep(2, 6),
    y1 = c(
      75.4,
      79.9,
      48.2,
      45.3,
      40.0,
      28.2
    ), #ub on treat
    y2 = NA,
    xstar = rep(1.5, 6),
    ystar = NA,
    est = c(
      mean(ds$therm[ds$t_te == 1 & ds$t_si == 0 & ds$ideo > 3], na.rm = TRUE) -
        mean(ds$therm[ds$t_te == 0 & ds$t_si == 0 & ds$ideo > 3], na.rm = TRUE),
      mean(ds$therm[ds$t_te == 1 & ds$t_si == 1 & ds$ideo > 3], na.rm = TRUE) -
        mean(ds$therm[ds$t_te == 0 & ds$t_si == 1 & ds$ideo > 3], na.rm = TRUE),
      mean(ds$therm[ds$t_te == 1 & ds$t_si == 0 & ds$ideo == 3], na.rm = TRUE) -
        mean(
          ds$therm[ds$t_te == 0 & ds$t_si == 0 & ds$ideo == 3],
          na.rm = TRUE
        ),
      mean(ds$therm[ds$t_te == 1 & ds$t_si == 1 & ds$ideo == 3], na.rm = TRUE) -
        mean(
          ds$therm[ds$t_te == 0 & ds$t_si == 1 & ds$ideo == 3],
          na.rm = TRUE
        ),
      mean(ds$therm[ds$t_te == 1 & ds$t_si == 0 & ds$ideo < 3], na.rm = TRUE) -
        mean(ds$therm[ds$t_te == 0 & ds$t_si == 0 & ds$ideo < 3], na.rm = TRUE),
      mean(ds$therm[ds$t_te == 1 & ds$t_si == 1 & ds$ideo < 3], na.rm = TRUE) -
        mean(ds$therm[ds$t_te == 0 & ds$t_si == 1 & ds$ideo < 3], na.rm = TRUE)
    ),
    p = c(
      t.test(
        ds$therm[ds$t_te == 1 & ds$t_si == 0 & ds$ideo > 3],
        ds$therm[ds$t_te == 0 & ds$t_si == 0 & ds$ideo > 3]
      )$p.value,
      t.test(
        ds$therm[ds$t_te == 1 & ds$t_si == 1 & ds$ideo > 3],
        ds$therm[ds$t_te == 0 & ds$t_si == 1 & ds$ideo > 3]
      )$p.value,
      t.test(
        ds$therm[ds$t_te == 1 & ds$t_si == 0 & ds$ideo == 3],
        ds$therm[ds$t_te == 0 & ds$t_si == 0 & ds$ideo == 3]
      )$p.value,
      t.test(
        ds$therm[ds$t_te == 1 & ds$t_si == 1 & ds$ideo == 3],
        ds$therm[ds$t_te == 0 & ds$t_si == 1 & ds$ideo == 3]
      )$p.value,
      t.test(
        ds$therm[ds$t_te == 1 & ds$t_si == 0 & ds$ideo < 3],
        ds$therm[ds$t_te == 0 & ds$t_si == 0 & ds$ideo < 3]
      )$p.value,
      t.test(
        ds$therm[ds$t_te == 1 & ds$t_si == 1 & ds$ideo < 3],
        ds$therm[ds$t_te == 0 & ds$t_si == 1 & ds$ideo < 3]
      )$p.value
    ),
    cand_ideo = rep(
      c('Moderately\nConservative', 'Extremely\nConservative'),
      3
    ),
    ideo_group = c(
      'Conservative Sample',
      'Conservative Sample',
      'Moderate Sample',
      'Moderate Sample',
      'Liberal Sample',
      'Liberal Sample'
    )
  ) %>%
  mutate(
    y1 = y1 + 3,
    y2 = y1 + 3,
    ystar = y2 + 5,
    lab = paste0("p = ", format(round(p, 2), nsmall = 2)),
    lab = ifelse(p < 0.01, "p < 0.01", lab),
    #lab = paste0(format(round(est,2),nsmall=2)," (p = ",format(round(p,2),nsmall=2),")"),
    #lab = ifelse(p<0.01, paste0(format(round(est,2),nsmall=2)," (p < 0.01)"), lab),
    ideo = factor(
      ideo_group,
      levels = c('Conservative Sample', 'Moderate Sample', 'Liberal Sample'),
      labels = c('Conservative Sample', 'Moderate Sample', 'Liberal Sample')
    ),
    cand_ideo = factor(
      cand_ideo,
      levels = c('Moderately\nConservative', 'Extremely\nConservative'),
      labels = c('Moderately\nConservative', 'Extremely\nConservative')
    )
  )


ds %>%
  filter(!is.na(therm), !is.na(treatment), !is.na(ideo)) %>%
  mutate(
    ideo = case_when(
      ideo < 3 ~ "Liberal Sample",
      ideo == 3 ~ "Moderate Sample",
      ideo > 3 ~ "Conservative Sample"
    )
  ) %>%
  group_by(treatment, ideo) %>%
  summarise(
    mean = mean(therm),
    se = sd(therm) / sqrt(length(therm)),
    lb = mean - 1.96 * se,
    ub = mean + 1.96 * se
  ) %>%
  mutate(
    cand_ideo = case_when(
      treatment %in% c('noMOD', 'trumpMOD') ~ 'mod',
      treatment %in% c('noEX', 'trumpEX') ~ 'ex'
    ),
    endorsed = case_when(
      treatment %in% c('noMOD', 'noEX') ~ '0',
      treatment %in% c('trumpMOD', 'trumpEX') ~ '1'
    ),
    cand_ideo = factor(
      cand_ideo,
      levels = c('mod', 'ex'),
      labels = c('Moderately\nConservative', 'Extremely\nConservative')
    ),
    ideo = factor(
      ideo,
      levels = c("Conservative Sample", "Moderate Sample", "Liberal Sample")
    )
  ) %>%
  ggplot() +
  geom_hline(yintercept = 50, color = 'grey30', linetype = "dashed") +
  geom_errorbar(
    aes(x = endorsed, y = mean, ymin = lb, ymax = ub, color = ideo),
    width = 0,
    linewidth = 1,
    alpha = .4
  ) +
  geom_point(aes(x = endorsed, y = mean, color = ideo), size = 2.5) +
  geom_text(
    aes(
      x = endorsed,
      y = lb,
      label = format(round(mean, 2), nsmall = 2),
      color = ideo
    ),
    size = 3,
    vjust = 1.5
  ) +
  geom_text(
    data = anno.df3,
    aes(x = xstar, y = ystar, label = lab),
    size = 3,
    color = "grey30"
  ) +
  geom_segment(
    data = anno.df3,
    aes(x = x1, xend = x1, y = y1, yend = y2),
    colour = "grey30"
  ) +
  geom_segment(
    data = anno.df3,
    aes(x = x2, xend = x2, y = y1, yend = y2),
    colour = "grey30"
  ) +
  geom_segment(
    data = anno.df3,
    aes(x = x1, xend = x2, y = y2, yend = y2),
    colour = "grey30"
  ) +
  theme_bw() +
  theme(legend.position = 'none') +
  facet_nested(~ ideo + cand_ideo) +
  scale_y_continuous(limits = c(0, 100)) +
  scale_color_manual(values = c('red', 'purple', 'blue')) +
  labs(
    x = 'Treatment: Trump Endorsement',
    y = 'Mean Thermometer Rating (0-100)',
    caption = 'Results shown by sample and assignment to candidate ideology, endorsement status.'
  ) +
  theme(
    strip.background = element_blank(),
    ggh4x.facet.nestline = element_line(colour = "black"),
    plot.caption = element_text(hjust = 0, vjust = -1)
  )

#### Figure A5 (DV = Perceived Ideology, by = pid) ----
anno.df4 <-
  data.frame(
    x1 = rep(1, 6),
    x2 = rep(2, 6),
    y1 = c(
      3.91,
      4.46,
      4.25,
      4.61,
      4.34,
      4.54
    ), #ub on treat
    y2 = NA,
    xstar = rep(1.5, 6),
    ystar = NA,
    est = c(
      mean(
        ds$cand_ideo[ds$t_te == 1 & ds$t_si == 0 & ds$pid == "rep"],
        na.rm = TRUE
      ) -
        mean(
          ds$cand_ideo[ds$t_te == 0 & ds$t_si == 0 & ds$pid == "rep"],
          na.rm = TRUE
        ),
      mean(
        ds$cand_ideo[ds$t_te == 1 & ds$t_si == 1 & ds$pid == "rep"],
        na.rm = TRUE
      ) -
        mean(
          ds$cand_ideo[ds$t_te == 0 & ds$t_si == 1 & ds$pid == "rep"],
          na.rm = TRUE
        ),
      mean(
        ds$cand_ideo[ds$t_te == 1 & ds$t_si == 0 & ds$pid == "ind"],
        na.rm = TRUE
      ) -
        mean(
          ds$cand_ideo[ds$t_te == 0 & ds$t_si == 0 & ds$pid == "ind"],
          na.rm = TRUE
        ),
      mean(
        ds$cand_ideo[ds$t_te == 1 & ds$t_si == 1 & ds$pid == "ind"],
        na.rm = TRUE
      ) -
        mean(
          ds$cand_ideo[ds$t_te == 0 & ds$t_si == 1 & ds$pid == "ind"],
          na.rm = TRUE
        ),
      mean(
        ds$cand_ideo[ds$t_te == 1 & ds$t_si == 0 & ds$pid == "dem"],
        na.rm = TRUE
      ) -
        mean(
          ds$cand_ideo[ds$t_te == 0 & ds$t_si == 0 & ds$pid == "dem"],
          na.rm = TRUE
        ),
      mean(
        ds$cand_ideo[ds$t_te == 1 & ds$t_si == 1 & ds$pid == "dem"],
        na.rm = TRUE
      ) -
        mean(
          ds$cand_ideo[ds$t_te == 0 & ds$t_si == 1 & ds$pid == "dem"],
          na.rm = TRUE
        )
    ),
    p = c(
      t.test(
        ds$cand_ideo[ds$t_te == 1 & ds$t_si == 0 & ds$pid == "rep"],
        ds$cand_ideo[ds$t_te == 0 & ds$t_si == 0 & ds$pid == "rep"]
      )$p.value,
      t.test(
        ds$cand_ideo[ds$t_te == 1 & ds$t_si == 1 & ds$pid == "rep"],
        ds$cand_ideo[ds$t_te == 0 & ds$t_si == 1 & ds$pid == "rep"]
      )$p.value,
      t.test(
        ds$cand_ideo[ds$t_te == 1 & ds$t_si == 0 & ds$pid == "ind"],
        ds$cand_ideo[ds$t_te == 0 & ds$t_si == 0 & ds$pid == "ind"]
      )$p.value,
      t.test(
        ds$cand_ideo[ds$t_te == 1 & ds$t_si == 1 & ds$pid == "ind"],
        ds$cand_ideo[ds$t_te == 0 & ds$t_si == 1 & ds$pid == "ind"]
      )$p.value,
      t.test(
        ds$cand_ideo[ds$t_te == 1 & ds$t_si == 0 & ds$pid == "dem"],
        ds$cand_ideo[ds$t_te == 0 & ds$t_si == 0 & ds$pid == "dem"]
      )$p.value,
      t.test(
        ds$cand_ideo[ds$t_te == 1 & ds$t_si == 1 & ds$pid == "dem"],
        ds$cand_ideo[ds$t_te == 0 & ds$t_si == 1 & ds$pid == "dem"]
      )$p.value
    ),
    cand_ideo = rep(
      c('Moderately\nConservative', 'Extremely\nConservative'),
      3
    ),
    pid = c(
      'Republican Sample',
      'Republican Sample',
      'Independent Sample',
      'Independent Sample',
      'Democratic Sample',
      'Democratic Sample'
    )
  ) %>%
  mutate(
    y1 = y1 + 0.05,
    y2 = y1 + .05,
    ystar = y2 + .08,
    lab = paste0("p = ", format(round(p, 2), nsmall = 2)),
    lab = ifelse(p < 0.01, "p < 0.01", lab),
    #lab = paste0(format(round(est,2),nsmall=2)," (p = ",format(round(p,2),nsmall=2),")"),
    #lab = ifelse(p<0.01, paste0(format(round(est,2),nsmall=2)," (p < 0.01)"), lab),
    pid = factor(
      pid,
      levels = c(
        'Republican Sample',
        'Independent Sample',
        'Democratic Sample'
      ),
      labels = c('Republican Sample', 'Independent Sample', 'Democratic Sample')
    ),
    cand_ideo = factor(
      cand_ideo,
      levels = c('Moderately\nConservative', 'Extremely\nConservative'),
      labels = c('Moderately\nConservative', 'Extremely\nConservative')
    )
  )

ds %>%
  filter(!is.na(cand_ideo), !is.na(treatment), !is.na(pid)) %>%
  group_by(treatment, pid) %>%
  summarise(
    mean = mean(cand_ideo),
    se = sd(cand_ideo) / sqrt(length(cand_ideo)),
    lb = mean - 1.96 * se,
    ub = mean + 1.96 * se
  ) %>%
  mutate(
    cand_ideo = case_when(
      treatment %in% c('noMOD', 'trumpMOD') ~ 'mod',
      treatment %in% c('noEX', 'trumpEX') ~ 'ex'
    ),
    endorsed = case_when(
      treatment %in% c('noMOD', 'noEX') ~ '0',
      treatment %in% c('trumpMOD', 'trumpEX') ~ '1'
    ),
    pid = factor(
      pid,
      levels = c('rep', 'ind', 'dem'),
      labels = c('Republican Sample', 'Independent Sample', 'Democratic Sample')
    ),
    cand_ideo = factor(
      cand_ideo,
      levels = c('mod', 'ex'),
      labels = c('Moderately\nConservative', 'Extremely\nConservative')
    )
  ) %>%
  ggplot() +
  geom_errorbar(
    aes(x = endorsed, y = mean, ymin = lb, ymax = ub, color = pid),
    width = 0,
    linewidth = 1.5,
    alpha = .4
  ) +
  geom_point(aes(x = endorsed, y = mean, color = pid), size = 3) +
  geom_text(
    aes(
      x = endorsed,
      y = lb,
      label = format(round(mean, 2), nsmall = 2),
      color = pid
    ),
    size = 3,
    vjust = 1.5
  ) +
  geom_text(
    data = anno.df4,
    aes(x = xstar, y = ystar, label = lab),
    size = 3,
    color = "grey30"
  ) +
  geom_segment(
    data = anno.df4,
    aes(x = x1, xend = x1, y = y1, yend = y2),
    colour = "grey30"
  ) +
  geom_segment(
    data = anno.df4,
    aes(x = x2, xend = x2, y = y1, yend = y2),
    colour = "grey30"
  ) +
  geom_segment(
    data = anno.df4,
    aes(x = x1, xend = x2, y = y2, yend = y2),
    colour = "grey30"
  ) +
  theme_bw() +
  theme(legend.position = 'none') +
  facet_nested(~ pid + cand_ideo) +
  scale_y_continuous(limits = c(3, 5)) +
  scale_color_manual(values = c('grey30', 'red', 'purple', 'blue')) +
  labs(
    x = 'Treatment: Trump Endorsement',
    y = 'Perceived Ideology',
    caption = 'Results shown by sample and assignment to candidate ideology, endorsement status.'
  ) +
  theme(
    strip.background = element_blank(),
    ggh4x.facet.nestline = element_line(colour = "black"),
    plot.caption = element_text(hjust = 0, vjust = -1)
  )

## 5.2 Average marginal effects ----
### Table A18 (DV = Thermometer) ----
mod3 <- lm(therm ~ t_te * t_si * pid * ideo, ds)
#### Full sample ----
summary(margins(mod3, variables = "t_te"))
summary(margins(mod3, variables = "t_te", at = list(t_si = 0:1)))
summary(margins(
  mod3,
  variables = "t_te",
  at = list(pid = c("rep", "ind", "dem"))
))
summary(margins(mod3, variables = "t_te", at = list(ideo = 1:5)))
#### Moderate candidate sample ----
summary(margins(mod3, variables = "t_te", at = list(t_si = 0)))
summary(margins(
  mod3,
  variables = "t_te",
  at = list(t_si = 0, pid = c("rep", "ind", "dem"))
))
summary(margins(mod3, variables = "t_te", at = list(t_si = 0, ideo = 1:5)))
#### Extreme candidate sample ----
summary(margins(mod3, variables = "t_te", at = list(t_si = 1)))
summary(margins(
  mod3,
  variables = "t_te",
  at = list(t_si = 1, pid = c("rep", "ind", "dem"))
))
summary(margins(mod3, variables = "t_te", at = list(t_si = 1, ideo = 1:5)))

### Table A19 (DV = Perceived Ideology) ----
mod4 <- lm(cand_ideo ~ t_te * t_si * pid * ideo, ds)
#### Full sample ----
summary(margins(mod4, variables = "t_te"))
summary(margins(mod4, variables = "t_te", at = list(t_si = 0:1)))
summary(margins(
  mod4,
  variables = "t_te",
  at = list(pid = c("rep", "ind", "dem"))
))
summary(margins(mod4, variables = "t_te", at = list(ideo = 1:5)))
#### Moderate candidate sample ----
summary(margins(mod4, variables = "t_te", at = list(t_si = 0)))
summary(margins(
  mod4,
  variables = "t_te",
  at = list(t_si = 0, pid = c("rep", "ind", "dem"))
))
summary(margins(mod4, variables = "t_te", at = list(t_si = 0, ideo = 1:5)))
#### Extreme candidate sample ----
summary(margins(mod4, variables = "t_te", at = list(t_si = 1)))
summary(margins(
  mod4,
  variables = "t_te",
  at = list(t_si = 1, pid = c("rep", "ind", "dem"))
))
summary(margins(mod4, variables = "t_te", at = list(t_si = 1, ideo = 1:5)))

### Table A20 (DV = Thermometer) ----
#### Full sample ----
summary(margins(
  mod3,
  variables = "t_te",
  at = list(ideo = 1:5, pid = c("rep", "ind", "dem"))
))
#### Moderate candidate sample ----
summary(margins(
  mod3,
  variables = "t_te",
  at = list(t_si = 0, ideo = 1:5, pid = c("rep", "ind", "dem"))
))
#### Extreme candidate sample ----
summary(margins(
  mod3,
  variables = "t_te",
  at = list(t_si = 1, ideo = 1:5, pid = c("rep", "ind", "dem"))
))

### Table A21 (DV = Perceived Ideology) ----
#### Full sample ----
summary(margins(
  mod4,
  variables = "t_te",
  at = list(ideo = 1:5, pid = c("rep", "ind", "dem"))
))
#### Moderate candidate sample ----
summary(margins(
  mod4,
  variables = "t_te",
  at = list(t_si = 0, ideo = 1:5, pid = c("rep", "ind", "dem"))
))
#### Extreme candidate sample ----
summary(margins(
  mod4,
  variables = "t_te",
  at = list(t_si = 1, ideo = 1:5, pid = c("rep", "ind", "dem"))
))

## 5.3 Nonlinearity Allowance for Ideology Term ----
### Figure A6 (DV = Thermometer) ----
mod1.nonlin <- lm(
  therm ~ t_te * t_si * pid * ideo + t_te * t_si * pid * ideo2,
  ds %>% mutate(ideo2 = ideo^2)
)

data.frame(summary(margins(
  mod1.nonlin,
  variables = "t_te",
  at = list(t_si = 0:1, ideo = 1:5, ideo2 = c(1, 4, 9, 16, 25))
))) %>%
  filter(ideo == sqrt(ideo2)) %>%
  mutate(
    t_si = factor(t_si, levels = c(0, 1), labels = c("ModCand", "ExtCand"))
  ) %>%
  ggplot(
    .,
    aes(
      x = ideo,
      y = AME,
      ymin = lower,
      ymax = upper
    )
  ) +
  geom_hline(yintercept = 0, color = "red", linetype = "dashed") +
  geom_errorbar(width = .25) +
  geom_point() +
  geom_text(
    aes(y = lower, label = format(round(AME, 2), nsmall = 2)),
    vjust = 1.5
  ) +
  scale_y_continuous(limits = c(-50, 50)) +
  facet_wrap(~t_si) +
  theme_bw()

### Figure A7 (DV = Perceived Ideology) ----
mod2.nonlin <- lm(
  cand_ideo ~ t_te * t_si * pid * ideo + t_te * t_si * pid * ideo2,
  ds %>% mutate(ideo2 = ideo^2)
)

data.frame(summary(margins(
  mod2.nonlin,
  variables = "t_te",
  at = list(t_si = 0:1, ideo = 1:5, ideo2 = c(1, 4, 9, 16, 25))
))) %>%
  filter(ideo == sqrt(ideo2)) %>%
  mutate(
    t_si = factor(t_si, levels = c(0, 1), labels = c("ModCand", "ExtCand"))
  ) %>%
  ggplot(
    .,
    aes(
      x = ideo,
      y = AME,
      ymin = lower,
      ymax = upper
    )
  ) +
  geom_hline(yintercept = 0, color = "red", linetype = "dashed") +
  geom_errorbar(width = .25) +
  geom_point() +
  geom_text(
    aes(y = lower, label = format(round(AME, 2), nsmall = 2)),
    vjust = 1.5
  ) +
  scale_y_continuous(limits = c(-2, 2)) +
  facet_wrap(~t_si) +
  theme_bw()

## 5.4 Treatment Effects by Trump-related Covariates ----
#### Figure A8 ----
ds %>%
  filter(!is.na(pid), !is.na(vote2020)) %>%
  group_by(pid, vote2020) %>%
  count() %>%
  ggplot(.) +
  geom_bar(aes(x = vote2020, y = n), stat = "identity") +
  geom_text(aes(x = vote2020, y = n, label = n), vjust = -1) +
  facet_wrap(~pid) +
  scale_y_continuous(limits = c(0, 400)) +
  theme_bw()

#### Figure A9 ----
mod1.vote <- lm(therm ~ t_te * t_si * pid * ideo + t_te * vote2020 * pid, ds)
m1v <- summary(margins(
  mod1.vote,
  variables = "t_te",
  at = list(
    pid = c("rep", "ind", "dem"),
    vote2020 = c("Trump", "Biden", "Did not vote")
  )
))

data.frame(m1v) %>%
  ggplot(
    .,
    aes(
      x = vote2020,
      y = AME,
      ymin = lower,
      ymax = upper
    )
  ) +
  geom_hline(yintercept = 0, color = "red", linetype = "dashed") +
  geom_errorbar(width = .25) +
  geom_point() +
  geom_text(
    aes(y = lower, label = format(round(AME, 2), nsmall = 2)),
    vjust = 1.5
  ) +
  scale_y_continuous(limits = c(-100, 100)) +
  facet_wrap(~pid) +
  theme_bw()


#### Figure A10 ----
ds %>%
  filter(!is.na(pid)) %>%
  ggplot(.) +
  geom_histogram(
    aes(trumpFT, fill = pid),
    color = "black",
    binwidth = 25,
    boundary = 0
  ) +
  facet_wrap(~pid) +
  labs(x = "Feeling Thermometer: Trump") +
  theme_bw() +
  theme(legend.position = "none")

#### Figure A11 ----
mod1.therm <- lm(therm ~ t_te * t_si * pid * ideo * trumpFT, ds)
m1t <- summary(margins(
  mod1.therm,
  variables = "t_te",
  at = list(pid = c("rep", "ind", "dem"), trumpFT = seq(0, 100, 5))
))

data.frame(m1t) %>%
  ggplot(
    .,
    aes(
      x = trumpFT,
      y = AME,
      ymin = lower,
      ymax = upper
    )
  ) +
  geom_hline(yintercept = 0, color = "red", linetype = "dashed") +
  geom_errorbar(width = .25) +
  geom_point() +
  scale_y_continuous(limits = c(-100, 100)) +
  facet_wrap(~pid) +
  theme_bw() +
  labs(x = "Feeling Thermometer: Trump")

#### Figure A12 ----
ds %>%
  mutate(
    trump_id = ifelse(CC22_340d <= 7, CC22_340d, NA),
    trump_id = factor(trump_id)
  ) %>%
  filter(!is.na(trump_id)) %>%
  ggplot(.) +
  geom_bar(aes(trump_id)) +
  labs(x = "Perception of Trump's Ideology")

#### Figure A13 ----
ds %>%
  mutate(
    trump_id = ifelse(CC22_340d <= 7, CC22_340d, NA),
    t_te = factor(t_te)
  ) %>%
  filter(!is.na(trump_id)) %>%
  group_by(t_te, trump_id) %>%
  summarise(
    mean = mean(cand_ideo, na.rm = TRUE),
    se = sd(cand_ideo, na.rm = TRUE) / sqrt(length(cand_ideo)),
    lower = mean - 1.96 * se,
    upper = mean + 1.96 * se
  ) %>%
  ggplot(.) +
  geom_errorbar(aes(t_te, mean, ymin = lower, ymax = upper), width = 0) +
  geom_point(aes(t_te, mean)) +
  geom_text(
    aes(t_te, mean, label = format(round(mean, 2), nsmall = 2)),
    hjust = -.5,
    size = 2.5
  ) +
  facet_wrap(~trump_id, scales = "free") +
  theme_bw() +
  labs(
    x = "Trump Endorsement",
    y = "Perceived Ideology of Candidate (1-5)",
    caption = "Only the difference in panel 7 reaches p<0.05"
  )

#### Figure A14 ----
data.frame(
  emmeans(
    lm(
      cand_ideo ~ t_te * t_si * pid * id_diff,
      ds %>%
        filter(CC22_340d <= 7, CC22_340a <= 7) %>%
        mutate(id_diff = CC22_340d - CC22_340a)
    ),
    ~ t_te * t_si * id_diff,
    at = list(id_diff = -3:6)
  )
) %>%
  mutate(
    t_si = factor(
      t_si,
      levels = 0:1,
      labels = c("Moderate Candidate", "Extreme Candidate")
    ),
    t_te = factor(
      t_te,
      levels = 0:1,
      labels = c("Control", "Trump\nEndorsement")
    )
  ) %>%
  ggplot(
    .,
    aes(
      x = t_te,
      y = emmean,
      group = t_si,
      color = t_si,
      shape = t_si
    )
  ) +
  geom_hline(yintercept = 50, color = "black", linetype = "dashed") +
  geom_line(alpha = .5) +
  geom_errorbar(
    aes(
      ymin = lower.CL,
      ymax = upper.CL
    ),
    width = .1,
    linewidth = 1,
    alpha = .5
  ) +
  geom_point(size = 2.5) +
  geom_text(
    aes(
      label = format(round(emmean, 2), nsmall = 2)
    ),
    size = 3,
    hjust = 1.5,
    data = . %>% filter(t_te == "Control"),
    show.legend = FALSE
  ) +
  geom_text(
    aes(
      label = format(round(emmean, 2), nsmall = 2)
    ),
    size = 3,
    hjust = -.5,
    data = . %>% filter(t_te == "Trump\nEndorsement"),
    show.legend = FALSE
  ) +
  facet_wrap(. ~ id_diff, nrow = 2) +
  scale_y_continuous(limits = c(2.5, 5.2), breaks = seq(3, 5, 1)) +
  scale_color_manual(values = c("black", "firebrick2")) +
  theme_bw() +
  theme(
    panel.grid.minor.y = element_blank(),
    legend.position = "bottom",
    panel.grid.major.x = element_blank()
  ) +
  labs(
    x = "",
    y = "Perceived Conservatism",
    color = "",
    shape = "",
    #title = "Perceived Conservatism of GOP Candidate (1-5)",
    subtitle = "by ideological distance between respondent and Trump (-3=Resp. is more conservative, +6=Trump is more conservative)"
  )

# 6 Mediation analysis ----
## 6.1 Sensitivity analysis ----
#### Figure A15 ----
med.1 <- lm(
  cand_ideo ~
    t_te +
      pid +
      ideo +
      CC22_340d +
      male +
      educ +
      age +
      white +
      hispanic +
      trump16 +
      trump20,
  ds %>%
    filter(!is.na(therm), !is.na(ideo))
)
out.1 <- lm(
  therm ~
    t_te +
      pid +
      ideo +
      CC22_340d +
      cand_ideo * ideo +
      male +
      educ +
      age +
      white +
      hispanic +
      trump16 +
      trump20,
  ds
)

set.seed(1234)
med.results1 <- mediate(
  med.1,
  out.1,
  treat = "t_te",
  mediator = "cand_ideo",
  sims = 1000,
  boot = TRUE,
  boot.ci.type = 'bca',
  conf.level = c(.95)
)

full.med <- summary(med.results1)


plot(
  medsens(
    med.results1,
    rho.by = 0.1,
    effect.type = 'indirect',
    sims = 10000
  ),
  sens.par = 'rho'
)

plot(
  medsens(
    med.results1,
    rho.by = 0.1,
    effect.type = 'indirect',
    sims = 10000
  ),
  sens.par = 'R2',
  r.type = 'total',
  sign.prod = 'positive'
)


#### Figure A16 ----
med.2 <- lm(
  cand_ideo ~
    t_te +
      pid +
      ideo +
      CC22_340d +
      male +
      educ +
      age +
      white +
      hispanic +
      trump16 +
      trump20,
  ds %>%
    filter(t_si == 0) %>%
    filter(!is.na(therm), !is.na(ideo))
)
out.2 <- lm(
  therm ~
    t_te +
      pid +
      ideo +
      CC22_340d +
      cand_ideo * ideo +
      male +
      educ +
      age +
      white +
      hispanic +
      trump16 +
      trump20,
  ds %>% filter(t_si == 0)
)

set.seed(1234)
med.results2 <- mediate(
  med.2,
  out.2,
  treat = "t_te",
  mediator = "cand_ideo",
  sims = 1000,
  boot = TRUE,
  boot.ci.type = 'bca',
  conf.level = c(.95)
)

mod.med <- summary(med.results2)

plot(
  medsens(
    med.results2,
    rho.by = 0.1,
    effect.type = 'indirect',
    sims = 10000
  ),
  sens.par = 'rho'
)

plot(
  medsens(
    med.results2,
    rho.by = 0.1,
    effect.type = 'indirect',
    sims = 10000
  ),
  sens.par = 'R2',
  r.type = 'total',
  sign.prod = 'positive'
)

#### Figure A17 ----
med.3 <- lm(
  cand_ideo ~
    t_te +
      pid +
      ideo +
      CC22_340d +
      male +
      educ +
      age +
      white +
      hispanic +
      trump16 +
      trump20,
  ds %>%
    filter(t_si == 1) %>%
    filter(!is.na(therm), !is.na(ideo))
)

out.3 <- lm(
  therm ~
    t_te +
      pid +
      ideo +
      CC22_340d +
      cand_ideo * ideo +
      male +
      educ +
      age +
      white +
      hispanic +
      trump16 +
      trump20,
  ds %>% filter(t_si == 1)
)

set.seed(1234)
med.results3 <- mediate(
  med.3,
  out.3,
  treat = "t_te",
  mediator = "cand_ideo",
  sims = 1000,
  boot = TRUE,
  boot.ci.type = 'bca',
  conf.level = c(.95)
)

ext.med <- summary(med.results3)


plot(
  medsens(
    med.results3,
    rho.by = 0.1,
    effect.type = 'indirect',
    sims = 10000
  ),
  sens.par = 'rho'
)

plot(
  medsens(
    med.results3,
    rho.by = 0.1,
    effect.type = 'indirect',
    sims = 10000
  ),
  sens.par = 'R2',
  r.type = 'total',
  sign.prod = 'positive'
)

# 7 Observational analysis ----
## 7.1 Ideological distribution of Trump-endorsed Republican House candidates ----
### Figure A18 ----
hrc22_full %>%
  mutate(te = factor(te)) %>%
  ggplot(., aes(pvi, recipient.cfscore)) +
  geom_hline(yintercept = 0, color = "grey50", linetype = "dashed") +
  geom_vline(xintercept = 0, color = "grey50", linetype = "dashed") +
  geom_point(aes(color = te)) +
  geom_smooth(method = "lm") +
  scale_x_continuous(limits = c(-40, 40)) +
  scale_y_continuous(limits = c(-.5, 2.0)) +
  scale_color_manual(values = c("black", "red")) +
  labs(x = "Cook PVI", y = "CFscore", color = "Trump\nEndorsement") +
  theme_bw()

### Figure A19 ----
hrc22_full %>%
  mutate(te = factor(te)) %>%
  ggplot(.) +
  geom_density(aes(recipient.cfscore, color = te, group = te)) +
  scale_color_manual(values = c("black", "red")) +
  labs(x = "CFscore", y = "density", color = "Trump\nEndorsement") +
  theme_bw() +
  annotate(
    "text",
    x = -1.5,
    y = 1.5,
    label = glue(
      "Mean CFscore: {format(round(mean(hrc22_full$recipient.cfscore[hrc22_full$te==0], na.rm = TRUE), 2), nsmall = 2)}"
    ),
    color = "black",
    hjust = 0
  ) +
  annotate(
    "text",
    x = -1.5,
    y = 1.4,
    label = glue(
      "Mean CFscore: {format(round(mean(hrc22_full$recipient.cfscore[hrc22_full$te==1], na.rm = TRUE), 2), nsmall = 2)}"
    ),
    color = "red",
    hjust = 0
  )

hrc22_full %>%
  mutate(te = factor(te)) %>%
  ggplot(.) +
  geom_density(aes(dwnom1, color = te, group = te)) +
  scale_color_manual(values = c("black", "red")) +
  labs(x = "dwnom1", y = "density", color = "Trump\nEndorsement") +
  theme_bw() +
  annotate(
    "text",
    x = -.25,
    y = 2,
    label = glue(
      "Mean dwnom1: {format(round(mean(hrc22_full$dwnom1[hrc22_full$te==0], na.rm = TRUE), 2), nsmall = 2)}"
    ),
    color = "black",
    hjust = 0
  ) +
  annotate(
    "text",
    x = -.25,
    y = 1.85,
    label = glue(
      "Mean dwnom1: {format(round(mean(hrc22_full$dwnom1[hrc22_full$te==1], na.rm = TRUE), 2), nsmall = 2)}"
    ),
    color = "red",
    hjust = 0
  )

### Figure A20 ----
diff_df <- data.frame(
  abspvi = 1:40,
  meanctrl = rep(NA, 40),
  meantrt = rep(NA, 40)
)
for (i in 1:40) {
  diff_df[i, 2] <- mean(
    hrc22_full$recipient.cfscore[hrc22_full$te == 0 & abs(hrc22_full$pvi) <= i],
    na.rm = TRUE
  )
  diff_df[i, 3] <- mean(
    hrc22_full$recipient.cfscore[hrc22_full$te == 1 & abs(hrc22_full$pvi) <= i],
    na.rm = TRUE
  )
}

diff_df %>%
  ggplot(.) +
  geom_point(aes(abspvi, meanctrl), color = "black") +
  geom_line(aes(abspvi, meanctrl), color = "black") +
  geom_point(aes(abspvi, meantrt), color = "red") +
  geom_line(aes(abspvi, meantrt), color = "red") +
  theme_bw() +
  labs(x = "Absolute value of Cook PVI", y = "Average CFscore") +
  annotate(
    "text",
    x = 11,
    y = 1.29,
    label = glue("Not endorsed by Trump"),
    color = "black",
    hjust = 0
  ) +
  annotate(
    "text",
    x = 3,
    y = 1.35,
    label = glue("Endorsed by Trump"),
    color = "red",
    hjust = 0
  )

#### Table A22 ----
ces %>%
  mutate(
    lic = case_when(
      low_info_cand == 1 ~ 'Low Information',
      low_info_cand == 0 ~ 'Not Low Information'
    ),
    te = as.factor(te)
  ) %>%
  filter(!is.na(lic), !is.na(ideo_r)) %>%
  group_by(lic, te, stcd_code) %>%
  summarise(
    mean = mean(ideo_r, na.rm = TRUE),
    var = var(ideo_r, na.rm = TRUE),
    sd = sd(ideo_r, na.rm = TRUE),
    n = length(ideo_r),
    se = sd / sqrt(n)
  ) %>%
  group_by(lic, te) %>%
  summarise(
    n_d = length(mean),
    var_ad = mean(var),
    sd_ad = mean(sd),
    var_bd = var(mean),
    sd_bd = sd(mean)
  )
